import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uikit/riki_uikit.dart';
import "package:uikit/utils/text_util.dart";
import 'package:uikit_example/pages/demo/register/widgets/login_input.dart';

import '../view_model/login_view_model.dart';
import '../widgets/login_input.dart';
import 'login_base_state.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends LoginBaseState<LoginPage, LoginViewModel> {
  String? userName;
  String? password;

  @override
  String get appBarTitle => "登录";

  @override
  String get bottomTitle => "登录";

  @override
  String get rightTitle => "注册";

  @override
  LoginViewModel get viewModel => Provider.of<LoginViewModel>(context, listen: false);

  @override
  List<Widget> buildContentChildren(BuildContext context) {
    return [
      LoginInput(
        '用户名',
        '请输入用户名',
        onChanged: (String text) {
          userName = text;
          checkInput();
        },
      ),
      LoginInput(
        '密码',
        '请输入密码',
        obscureText: true,
        onChanged: (String text) {
          password = text;
          checkInput();
        },
        focusChanged: (bool value) {
          viewModel.isProtected = value;
        },
      )
    ];
  }

  /// 检查输入框输入
  void checkInput() {
    bool result1 = TextUtil.isEmpty(userName);
    bool result2 = TextUtil.isEmpty(password);
    if (result1 || result2) {
      viewModel.loginEnable = false;
    } else {
      viewModel.loginEnable = true;
    }
  }

  /// 检查参数
  @override
  void checkParams() {
    print("sendRegisterRequest begin");
    viewModel.sendUserRequest(userName!, password!);
    print("sendRegisterRequest end");
  }

  @override
  void pushNextPage() {
    Navigator.of(context).pushNamed("/Register");
  }
}
